package com.lifl.b328.garden.model;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Tools {

	/**
	 * @param d
	 * @return string
	 */
	public static String dateToString(Date d){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
	}
	
	/**
	 * 
	 * @param sDate
	 * @return
	 */
	
	public static Date stringToDate(String sDate){
        Date d = null;
        try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ParsePosition pos = new ParsePosition(0);
			d = formatter.parse(sDate,pos);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return d;	 	
	}
	
	public static String printJson(User user,Note note, long userID, long noteID,String action) {
		String userJson = "";
		String noteJson = "";
		String virgule="";
		
		String email = user.getEmail();
		String token = user.getToken();
		userJson = "\"user\" : {" +
				" \"id\": "+userID +","+
				" \"email\":\""+email+"\","+
				" \"token\":\""+ token+"\"}";
		
		if(note != null){
			String content = ((TextContent)note.getContentByKey(Constantes.NOTE_CONTENT)).getText();
			String creation = Tools.dateToString(((DateContent) note.getContentByKey(Constantes.NOTE_CREATION)).getDate());
			String edition =  Tools.dateToString(((DateContent) note.getContentByKey(Constantes.NOTE_EDITION)).getDate());
			CoordonneesContent gps = (CoordonneesContent) note.getContentByKey(Constantes.NOTE_GPS);
			double longitude = gps.getLongitude();
			double latitude = gps.getLatitude();
			
			noteJson = 
					"\"content\" :\"" +content+"\","+
					"\"creation\" :\""+creation+"\","+
					"\"edition\" :\""+edition+"\","+
					"\"latitude\" :"+latitude+","+
					"\"longitude\" :"+longitude;
			virgule = ",";
		}
		String json = "{ "+"\"action\" :\"" + action +"\","+ userJson +  ",\"note\" : {\"id\" :"+noteID +virgule+ noteJson +"} }";
		return json;
	}
}
